package com.ys.everydaynews.ui.main.activityy

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.entity.LocalMedia
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseActivity
import com.ys.everydaynews.databinding.ActivityUpdataUserInfoBinding
import com.ys.everydaynews.mvp.view.BaseView
import com.ys.everydaynews.ui.main.presenter.UserInfoUpdataPresenter
import com.ys.everydaynews.view.DialogFragmentListener
import com.ys.everydaynews.view.Photo_Dialog
import com.ys.everydaynews.view.UserInfoItemView
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.ys.everydaynews.utils.GlideEngine
import com.ys.everydaynews.utils.ImageLoaderUtil
import com.ys.everydaynews.view.EditTextDialog


class UserInfoUpdataActivity :
    BaseActivity<BaseView, UserInfoUpdataPresenter, ActivityUpdataUserInfoBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    override fun getLayoutId(): Int = R.layout.activity_updata_user_info

    override fun initView() {
        val mphoto = Photo_Dialog()
        binding.itemHead.setUserOnClickListener(object : UserInfoItemView.UserOnClickListener {
            override fun userOnClick(view: View) {
                mphoto.show(supportFragmentManager, "photodialog")
                mphoto.setDialogFragmentListener(object : DialogFragmentListener {
                    override fun onDialog(type: Int) {
                        when (type) {
                            1 -> selectPhoto()
                            2 -> setCaream()
                            3 -> mphoto.dismiss()
                        }
                    }

                })
            }
        })
        binding.itemName.setUserOnClickListener(object : UserInfoItemView.UserOnClickListener{
            override fun userOnClick(view: View) {
                EditTextDialog("请输入用户名").show(supportFragmentManager,"editdialog")
            }
        })
    }

    override fun initData() {
        binding.itemName.setTextRight("用户姓名")
        binding.itemSex.setTextRight("男")
        binding.itemWhere.setTextRight("待完善")
        binding.itemWhere.setTxtRightColor(R.color.purple_700)
    }

    private fun selectPhoto() {
        PictureSelector.create(this)
            .openGallery(PictureMimeType.ofAll())
            .imageEngine(GlideEngine.createGlideEngine())
            .selectionMode(PictureConfig.SINGLE)
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: List<LocalMedia?>) {
                    binding.itemHead.setImageUrl(result[0]!!.cutPath)
                }

                override fun onCancel() {
                    // 取消
                }
            })
    }

    private fun setCaream() {
        PictureSelector.create(this)
            .openCamera(PictureMimeType.ofImage())
            .imageEngine(GlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: List<LocalMedia?>) {
                    // 结果回调
                    binding.itemHead.setImageUrl(result[0]!!.cutPath)
                }

                override fun onCancel() {
                    // 取消
                }
            })
    }

    override fun createPersenter(): UserInfoUpdataPresenter = UserInfoUpdataPresenter()

    override fun isRegister(): Boolean = false

    override fun <T> setData(data: T) {
    }

    override fun setError(err: String) {
    }

}